<?php
class CategoriesController extends AppController {

	var $name = 'Categories';
	var $helpers = array('Tree');
	var $components = array('Site');

	function index() {
		$this->Category->recursive = 0;
		$this->set('categorytree', $this->Category->findAllThreaded("Category.type=1", null, 'Category.order ASC'));
	}

	function view($cat = null) {
		$this->Category->recursive = -1;
    if ($cat && $category = $this->Category->findByName(strtolower($cat))) {
      $this->set('category', $category);
			$ispublic=$category['Category']['type']==1;
			$this->set('ispublic', $ispublic);
			$this->set('categorychildren',$this->Category->catChildren($category['Category']['id']));
			$this->Category->recursive = 0;
			$this->Site->getPath($category['Category']['id']);
			if ($this->params['webservices'] == 'Rss') {
				$fields=array('Post.id','Post.title','Post.content','Post.replies','Post.closed','Post.created','Post.modified','User.login','User.name');
				$this->pageTitle="Groups - ".$category['Category']['description'];
			} else {
			$fields=array('Post.id','Post.title','Post.replies','Post.closed','Post.created','Post.modified','Post.points','Post.rated','User.login','User.name');
			$this->set('rsscat',$category['Category']['name']);
			}
			$this->Category->Post->unbindModel(array('belongsTo' => array('Category')));
			$cond=array('Post.category_id'=>$category['Category']['id']);
			// sorting
			list($sort,$order)=$this->Site->getSort('modified');
   		$count=$category['Category']['count'];
			$posts=$this->Category->Post->findAll($cond,$fields,$sort." ".$order,PAGESIZE,$this->pager($count,PAGESIZE),0);
			$this->set('posts', $posts);
			$this->set('count', $count);

			if($ispublic) {
				$bestusers=$this->Category->bestUsers($category['Category']['id']);
				$this->set('bestusers', $bestusers);
			}
    } else {
      	$this->Session->setFlash('There is no such group.','msgerror');
				$this->redirect('/groups/');
    }
	}

    function admin_index() {
		$this->checkAdmin(); $this->layout='admin';
		$this->Category->recursive = 0;
		$this->set('categories', $this->Category->findAll(null, null, 'Category.parent_id ASC'));
		$this->set('categorytree', $this->Category->findAllThreaded(null, null, 'Category.order ASC'));
	}


	function admin_add() {
		$this->checkAdmin(); $this->layout='admin';
		$cpar=$this->Category->generateList(null, 'id ASC', null, '{n}.Category.id', '{n}.Category.name');
		$cpar['0']="root";

    Configure::load('vars');
		$this->set('ctype', Configure::read('ctype'));
    Configure::load('settings');
		$pnt=Configure::read('points');

		if(empty($this->data)) {
			$this->data['Category']['cost']=$pnt['writepost'];
			$this->set('cpar', $cpar);
		} else {
			$this->cleanUpFields();
			if($this->Category->save($this->data)) {
				$this->Session->setFlash('Group created.');
				$this->redirect('/admin/categories/');
			} else {
				$this->Session->setFlash('Fix errors.',"msgerror");
			}
		}
	}

	function admin_edit($id = null) {
		$this->checkAdmin(); $this->layout='admin';
    $cpar=$this->Category->generateList(null, 'id ASC', null, '{n}.Category.id', '{n}.Category.name');
		$cpar['0']="root";

    Configure::load('vars');
		$this->set('ctype', Configure::read('ctype'));
		if(empty($this->data)) {
			if(!$id) {
				$this->redirect('/admin/categories/');
			}
			$this->Category->recursive = 0;
			$this->data = $this->Category->read(null, $id);
			$this->data = $this->Category->read(null, $id);
			unset($cpar[$id]);
			$this->set('cpar', $cpar);

		} else {
			$this->cleanUpFields();
			if($this->Category->save($this->data)) {
				$this->Session->setFlash('Group modified.');
				$this->redirect('/admin/categories/');
			} else {
				$this->Session->setFlash('Fix errors.',"msgerror");
			}
		}
	}

	function admin_delete($id = null) {
		$this->checkAdmin(); $this->layout='admin';
		if(!$id) {
			$this->redirect('/admin/categories/');
			exit;
		}
		if($this->Category->del($id)) {
			$this->Session->setFlash('Group deleted: id '.$id.'');
			$this->redirect('/admin/categories/');
		}
	}

}
?>